import { HttpResponse } from '../services/httpClient';
import { MockHandler } from '../services/MockService';
import { User } from '../types';

// 模拟推荐用户数据
export const mockRecommendedUsers: User[] = [
  {
    id: 'user4',
    name: '赵六',
    avatar: 'https://randomuser.me/api/portraits/women/4.jpg',
    province: '广东省',
    city: '广州市',
    followed: false,
  },
  {
    id: 'user5',
    name: '孙七',
    avatar: 'https://randomuser.me/api/portraits/men/5.jpg',
    province: '湖北省',
    city: '武汉市',
    followed: false,
  },
  {
    id: 'user6',
    name: '周八',
    avatar: 'https://randomuser.me/api/portraits/women/6.jpg',
    province: '湖南省',
    city: '长沙市',
    followed: false,
  },
  {
    id: 'user7',
    name: '吴九',
    avatar: 'https://randomuser.me/api/portraits/men/7.jpg',
    province: '四川省',
    city: '成都市',
    followed: true,
  },
  {
    id: 'user8',
    name: '郑十',
    avatar: 'https://randomuser.me/api/portraits/women/8.jpg',
    province: '浙江省',
    city: '杭州市',
    followed: false,
  },
];

// 模拟关注用户数据
export const mockFollowings: User[] = [
  {
    id: 'user1',
    name: '张三',
    avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
    province: '浙江省',
    city: '杭州市',
    followed: true,
  },
  {
    id: 'user2',
    name: '李四',
    avatar: 'https://randomuser.me/api/portraits/women/2.jpg',
    province: '江苏省',
    city: '南京市',
    followed: true,
  },
  {
    id: 'user3',
    name: '王五',
    avatar: 'https://randomuser.me/api/portraits/men/3.jpg',
    province: '广东省',
    city: '深圳市',
    followed: true,
  },
];

// 模拟粉丝用户数据
export const mockFollowers: User[] = [
  {
    id: 'user9',
    name: '钱十一',
    avatar: 'https://randomuser.me/api/portraits/men/9.jpg',
    province: '浙江省',
    city: '杭州市',
    followed: false,
  },
  {
    id: 'user10',
    name: '孙十二',
    avatar: 'https://randomuser.me/api/portraits/women/10.jpg',
    province: '江苏省',
    city: '南京市',
    followed: true,
  },
  {
    id: 'user11',
    name: '李十三',
    avatar: 'https://randomuser.me/api/portraits/men/11.jpg',
    province: '广东省',
    city: '深圳市',
    followed: false,
  },
];

// 模拟当前登录用户数据
export const mockCurrentUser: User = {
  id: 'current_user_1',
  name: '肖如海',
  phone: '18688888888',
  avatar: 'https://randomuser.me/api/portraits/men/9.jpg',
  age: 25,
  province: '浙江省',
  city: '杭州市',
  country: '中国',
  gender: '男',
  description: '热爱户外运动的测试用户',
  occupation: '软件工程师',
  followed: false,
};

// 模拟认证token
let mockAuthToken: string | null = null;

export const userMockHandler: MockHandler = {
  async handleRequest<T>(
    url: string,
    options?: RequestInit,
  ): Promise<HttpResponse<T> | null> {
    console.log('User mock handler processing request:', url);

    // 处理用户登录请求
    if (url === '/auth/login') {
      return handleLoginRequest<T>(url, options);
    }
    // 处理用户登出请求
    else if (url === '/auth/logout') {
      return handleLogoutRequest<T>(url, options);
    }

    // 对于未处理的URL，返回null表示不拦截
    console.log('User mock handler skipping URL:', url);
    return null;
  },
};

// 处理用户登录请求
async function handleLoginRequest<T>(
  url: string,
  options?: RequestInit,
): Promise<HttpResponse<T>> {
  console.log('Handling user login request');

  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 500));

  // 解析请求体中的登录凭据
  let body = {};
  if (options?.body) {
    if (typeof options.body === 'string') {
      body = JSON.parse(options.body);
    } else if (typeof options.body === 'object') {
      body = options.body;
    }
  }

  const { phone, password } = body as { phone: string; password: string };

  // 简单验证凭据（在实际应用中应该有更复杂的验证逻辑）
  if (!phone || !password) {
    const errorResponse: HttpResponse<T> = {
      data: null as any,
      status: 400,
      statusText: 'Bad Request',
      headers: {
        'content-type': 'application/json',
      },
    };
    return errorResponse;
  }

  // 设置模拟认证token
  mockAuthToken = 'mock-jwt-token-' + Date.now();

  // 构造响应数据
  const responseData = {
    user: mockCurrentUser,
    token: mockAuthToken,
  };

  const response: HttpResponse<T> = {
    data: responseData as any,
    status: 200,
    statusText: 'OK',
    headers: {
      'content-type': 'application/json',
    },
  };

  return response;
}

// 处理用户登出请求
async function handleLogoutRequest<T>(
  url: string,
  options?: RequestInit,
): Promise<HttpResponse<T>> {
  console.log('Handling user logout request');

  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 300));

  // 清除模拟认证token
  mockAuthToken = null;

  const response: HttpResponse<T> = {
    data: null as any,
    status: 200,
    statusText: 'OK',
    headers: {
      'content-type': 'application/json',
    },
  };

  return response;
}
